'use strict';
module.exports = {
	/**
	 * 获取我的课程列表
	 * @url client/learn/pub/myCourseList 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			pageIndex = 1, pageSize = 10, uid,
				type

		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------

		if (uid == null) {
			res.code = -1
			res.msg = "请登录后查看"
			return res
		}

		let whereJson = {};
		whereJson['user_id'] = uid
		whereJson['goods_type'] = type
		let foreignDB = type == 'course' ? [{
			dbName: "course",
			localKey: "goods_id",
			foreignKey: "_id",
			as: "courseInfo",
			fieldJson: {
				render_desc: false
			},
			limit: 1,
			foreignDB: [{
				dbName: "course_chapter",
				localKey: "_id",
				foreignKey: "course_id",
				as: "chapter",
				fieldJson: {
					render_desc: false
				}
			}, {
				dbName: "course_videos",
				localKey: "_id",
				foreignKey: "course_id",
				as: "videos",
				fieldJson: {
					render_desc: false
				}
			}]
		}] : [{
			dbName: "live_course",
			localKey: "goods_id",
			foreignKey: "_id",
			as: "courseInfo",
			fieldJson: {
				render_desc: false
			},
			limit: 1,
			foreignDB: [{
				dbName: "course_chapter",
				localKey: "_id",
				foreignKey: "course_id",
				as: "chapter",
				fieldJson: {
					render_desc: false
				}
			}, {
				dbName: "live_course_videos",
				localKey: "_id",
				foreignKey: "course_id",
				as: "videos",
				fieldJson: {
					render_desc: false
				}
			}]
		}]
		res = await vk.baseDao.selects({
			dbName: "user_goods",
			whereJson: whereJson,
			pageIndex: pageIndex,
			pageSize: pageSize,
			sortArr: [{
				"name": "_add_time",
				"type": "desc"
			}],
			fieldJson: {
				render_desc: false
			},
			// 副表列表
			foreignDB,
			lastWhereJson: {
				"courseInfo._id": _.exists(true)
			}
		});


		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
